/*
 * Copyright (c) 2012 by Chances.
 * $CVSHeader$
 * $Author$
 * $Date$
 * $Revision$
 */
package chances.epg.api.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.velocity.tools.generic.DateTool;
import org.springframework.beans.factory.annotation.Autowired;

import chances.epg.api.tools.ContentTool;
import chances.epg.commons.utils.RequestUtils;
import chances.epg.commons.utils.URLUtils;
import chances.epg.config.EpgSiteContext;
import chances.epg.data.PageBean;
import chances.epg.data.service.CategoryService;
import chances.epg.data.service.ChannelService;
import chances.epg.data.service.ContentService;
import chances.epg.entity.content.CategoryItem;
import chances.epg.navigator.context.AccessContext;
import chances.epg.navigator.context.AccessContextUtils;
import chances.epg.navigator.service.AccessContextFactory;
import chances.epg.navigator.service.NavigateUrlFactory;

/**
 * Class comments.
 * 
 * @author <a href="mailto:libf@sitv.com.cn">libf</a>
 */
public abstract class AbstractDataController {

	public static final String VOD_TYPE = "vod";
	public static final String SERIES_TYPE = "series";
	public static final String SERIES2_TYPE = "series2";

	public static final int SUCCESS = 200;

	// 系统错误：9
	public static final int ERROR = 9999;
	public static final int DB_ERROR = 9001;
	public static final int DB_DUPLICATE = 9002;

	public static final int OBJ_NOT_FOUND = 511;
	public static final int SEARCH_ERROR = 512;
	public static final int NOT_SUPPORT_SEARCH = 513;

	protected static final int DEFAULT_SIZE = 20;
	protected static final DateTool DATETOOL = new DateTool();
	protected static final ContentTool CONTENTTOOL = new ContentTool();

	@Autowired
	protected CategoryService categoryService;

	@Autowired
	protected ContentService contentService;

	@Autowired
	protected ChannelService channelService;
	
	@Autowired
	protected NavigateUrlFactory navigateUrlFactory;

	protected PageBean createPageBean(int size, int curPage) {
		PageBean pageBean = new PageBean(size);
		pageBean.setCurrentPage(curPage);
		return pageBean;
	}

	public String getIpAddr(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if ((ip == null) || (ip.length() == 0) || ("unknown".equalsIgnoreCase(ip))) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if ((ip == null) || (ip.length() == 0) || ("unknown".equalsIgnoreCase(ip))) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if ((ip == null) || (ip.length() == 0) || ("unknown".equalsIgnoreCase(ip))) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}

	public CategoryService getCategoryService() {
		return categoryService;
	}

	public ContentService getContentService() {
		return contentService;
	}

	public ChannelService getChannelService() {
		return channelService;
	}
	
	public void setResultUrl(List<?> result, HttpServletRequest request) {
		int index = 0;
		AccessContext context = createAccessContext(request);
		for (Object object : result) {
			if(object instanceof CategoryItem) {
				CategoryItem categoryItem = (CategoryItem) object;
				index++;
				String url = navigateUrlFactory.generateURL(context, categoryItem);
				url = URLUtils.appendParam(url, "pi", index + "");
				categoryItem.setUrl(url);
			}
		}
	}
	
	public String getResultItemUrl(AccessContext context, Map<String, Object> item) {
		String url = navigateUrlFactory.generateSearchFilterURL(context, item);
		return url;
	}
	
	public AccessContext createAccessContext(HttpServletRequest request) {
		AccessContext context = AccessContextUtils.getAccessContext(request);
		if (context == null) {
			AccessContextFactory contextFactory = new AccessContextFactory();
			context = contextFactory.createAccessContext(request);
			String bizCode = RequestUtils.getParameter(request, "bizCode", "");
			String siteCode = RequestUtils.getParameter(request, "siteCode", "");
			String categoryCode = RequestUtils.getParameter(request, "categoryCode", "");
			if(StringUtils.isNotEmpty(categoryCode)) {
				context.setContainerCode(categoryCode);
			}
			EpgSiteContext epgSiteContext = new EpgSiteContext(siteCode);
			epgSiteContext.setContextPath(request.getContextPath());
			context.setBizCode(bizCode);
			context.setSiteCode(siteCode);
			context.setEpgSiteContext(epgSiteContext);
		}
		return context;
	}
	
}
